package com.taotao.rest.service;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.rest.dao.SearchDao;
import com.taotao.rest.pojo.SearchResult;

@Service
public class SearchServiceImpl implements SearchService {
	@Autowired
	private SearchDao searchDao;
	@Override
	public SearchResult getItemList(String queryString, int page, int rows) throws Exception {
		// TODO Auto-generated method stub
		SolrQuery query = new SolrQuery();
		
		query.setQuery(queryString);
		query.setStart((page-1)*rows);
		query.setRows(rows);
		query.set("df", "item_keywords");
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>");
		SearchResult searchResult = searchDao.search(query);
		
		long recordsCount = searchResult.getRecordsCounts();
		long pageCount = recordsCount / rows;
		//有余数就加一页
		if (recordsCount % rows >0 ) {
			pageCount ++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage(page);
		return searchResult;
	}

}
